home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2807 < prev    next >
Encoding:
Text File  |  1996-08-06  |  3.4 KB  |  90 lines

  1. Path: news.ssnet.com!not-for-mail
  2. From: helie@ssnet.com (Ray Helie)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Virtual Members: Difficult Question
  5. Date: 19 Jan 1996 14:53:39 -0500
  6. Organization: SSNet, Inc.
  7. Message-ID: <4doss3$b1i@marlin.ssnet.com>
  8. References: <4dmha6$80c@marlin.ssnet.com> <4doof9$hom@charnel.ecst.csuchico.edu>
  9. NNTP-Posting-Host: marlin.ssnet.com
  10.  
  11. James Robert McElroy <mcelroy@ecst.csuchico.edu> wrote:
  12. - A general comment about your code:
  13. - You are casting the wrong way in the inheritance hierarchy.
  14. - This is guaranteed to blow up at some point, often unpredic-
  15. - tably.  Some times it will work, some times it won't (similar
  16. - to driving the wrong way down a one-way street).
  17. - Look at it this way:  in terms of physical space taken up by
  18. - a class object, a base class object will always take up less room than 
  19. - a derived class object, right?  Now, if you tell a base class object it is
  20. - actually a derived class object (via the cast) the pointer now thinks
  21. - it has more room in memory than it actually does.  
  22.  
  23. Unless the base class object was *itself* gotten by casting it 
  24. from the larger derived object, which is what *I've* done..
  25. I understand what you're saying, but when the memory at that
  26. object's location was originally allocated, it /was/ of the
  27. larger size (the derived object's size)-- look at the sample
  28. code again, I think I stated it correctly.
  29.  
  30.  
  31. - Look at it this way -- would the following code cause  problems?
  32. - int foo;
  33. - long * fubar = (long*)&foo;
  34. - *fubar = 10;        // gee, no problem here
  35. - *fubar = 32468551;  // uh oh.  Seems to crash.  Don't know why!
  36. - Yup.  Similar to what you are experiencing. 
  37.  
  38.  
  39. You are right and I'm well aware of /that/ problem (who isn't?)-- your 
  40. example is not a match of my example.  Using what you just wrote, /this/ 
  41. is my problem (although not using objects, this example seems pointless):
  42.  
  43. int   foo;
  44. long  foobar;
  45. int*  pInt;
  46. long* pLong
  47.  
  48. foobar = 1234567;
  49. pInt = &foobar; // pointless, but it will not stop you
  50. // right now I have a pointer to a smaller object (pInt), but the
  51. // larger object's data (pLong) should still be there,
  52. // so the following line:
  53. pLong = pInt;
  54. // followed by
  55. printf ("%l",*pLine);  // should print out 1234567
  56. // should still work.  and that's what I'm doing in my program.
  57. // the memory for the biggest variable (ie., the derived class)
  58. // is already allocated, even though later I cast the pointer
  59. // to a smaller object (the base class), and it thinks there's
  60. // less memory there.  but there *is* more memory there
  61. // due to the *initial* definition.
  62.  
  63. // and now what fails for me is:
  64. char* pChar;
  65. pChar = pInt; // again pointless
  66. pLong = pChar;
  67. *pLong = 11223344;
  68. printf ("%l",*pLine); // this line fails, trying to show
  69. // similar problem I'm having.. it should have printed out
  70. // 11223344, but instead dies.
  71.  
  72. In a nutshell, I declared object of derived size, cast to the
  73. base, cast again to a parent class of base, cast back to base,
  74. tried to write a derived amount of data to the base class (as if
  75. it was an array of characters, starting at the object's address,
  76. which is the same way I wrote the information to begin with), and
  77. it dies on me when I do a virtual function call.  The derived amount of 
  78. space is there -- that's what I started with.  
  79.  
  80. So I don't think you understand my question, because I understand
  81. what you're saying, you are right under the circumstances you
  82. described, but I think you got the circumstances wrong...?
  83.  
  84.  
  85. If anyone else can help, I'd appreciate it.  :)
  86.